//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005-2009 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using System;
using System.Net;
using System.Net.Sockets;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// Thin wrapper for the <see cref="Socket"/> class.
	/// </summary>
	public class TcpSocket : ISocket
	{
		/// <summary>
		/// The underlying network socket.
		/// </summary>
		private Socket m_Socket;
		
		/// <summary>
		/// Creates a new instance of the <see cref="TcpSocket"/>
		/// class.</summary>
		/// <param name="addressFamily">One of the <see cref="AddressFamily"/>
		///  values.</param>
		/// <param name="socketType">One of the <see cref="SocketType"/>
		///  values.</param>
		/// <param name="protocolType">One of the <see cref="ProtocolType"/>
		///  values.</param>
		public TcpSocket( AddressFamily addressFamily,
			SocketType socketType,
			ProtocolType protocolType )
		{
			m_Socket = new Socket( addressFamily, socketType, protocolType );
		}
		
		/// <summary>
		/// Creates a new instance of the <see cref="TcpSocket"/> class.
		/// </summary>
		/// <param name="socket">A socket.</param>
		public TcpSocket( Socket socket )
		{
			m_Socket = socket;
		}
		
		/// <summary>
		/// Gets the first IPv4 address from the host entry's address list.
		/// </summary>
		public static IPAddress GetIPAddress( IPHostEntry HostEntry )
		{
			foreach( IPAddress Address in HostEntry.AddressList )
			{
				if( Address.AddressFamily == AddressFamily.InterNetwork )
				{
					return Address;
				}
			}
			return null;
		}
		
		/// <summary>
		/// Closes the socket.
		/// </summary>
		public void Close( )
		{
			m_Socket.Close( );
		}
		
		/// <summary>
		/// Establishes a connection to a remote host.
		/// </summary>
		/// <param name="remoteEP">An end point that represents
		/// the remote device.</param>
		public void Connect( EndPoint remoteEP )
		{
			m_Socket.Connect( remoteEP );
		}

		/// <summary>
		/// Determines the status of the Socket.
		/// </summary>
		/// <param name="microSeconds">The time to wait for a response, in 
		/// microseconds.</param>
		/// <param name="mode">One of the <see cref="SelectMode"/> 
		/// values. </param>
		public bool Poll( int microSeconds, SelectMode mode )
		{
			return m_Socket.Poll( microSeconds, mode );
		}
		
		/// <summary>
		/// Receives data from a bound Socket into a receive buffer.
		/// </summary>
		/// <param name="buffer">An array of type Byte that is the storage 
		/// location for the received data.
		/// </param>
		/// <returns>The number of bytes received.</returns>
		public int Receive( byte[ ] buffer )
		{
			return m_Socket.Receive( buffer );
		}

		/// <summary>
		/// Disables sends and receives on a socket.
		/// </summary>
		/// <param name="how">One of the SocketShutdown values that specifies 
		/// the operation that will no longer be allowed. </param>
		public void Shutdown( SocketShutdown how )
		{
			m_Socket.Shutdown( how );
		}

		/// <summary>
		/// Gets the remote endpoint
		/// </summary>
		public EndPoint RemoteEndPoint 
		{ 
			get
			{
				return m_Socket.RemoteEndPoint;
			}
		}
		
		/// <summary>
		/// Gets the local endpoint
		/// </summary>
		public EndPoint LocalEndPoint 
		{ 
			get
			{
				return m_Socket.LocalEndPoint;
			}
		}			
		
		/// <summary>
		/// Sends data to a connected Socket.
		/// </summary>
		/// <param name="buffer">An array of of type Byte that contains 
		/// the data to be sent. </param>
		public int Send( byte[ ] buffer )
		{
			return m_Socket.Send( buffer );
		}
	}
}
